<?php
/**
 * @ClassName: Customer
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-03 18:13
 * @Version: V1.0.0
 */

namespace app\test\controller;

use app\common\logic\CustomerLogic;
use think\Controller;
use app\common\model;

class Customer extends Controller
{
    /**
     * 根据会员信息创建客户
     * @return \think\response\Json
     * @throws \think\Exception
     */
    function createCustomerByMember()
    {
        $member_list = model\Member::select();

        $count = 0;
        foreach ($member_list as $member) {
            $customer = model\Customer::alias('c')->distinct('c.id')
                ->leftJoin('customer_contacts cc', 'cc.customer_id=c.id')
                ->where('cc.member_id', $member['id'])
                ->field('c.*')
                ->find();

            if (!$customer) {
                // 创建客户
                $res = (new CustomerLogic())->createCustomer($member['nickname'], $member['mobile'], $member['id']);
                if ($res['code'] == 1) {
                    $count++;
                }
            }
        }

        return json(['code' => 1, 'msg' => '共' . count($member_list) . '条数据，创建成功' . $count . '条']);
    }

}